package util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "Filter 0",urlPatterns = {"/*"})
public class Filter00_Encoding implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req,
                         ServletResponse resp,
                         FilterChain chain)
            throws IOException, ServletException {
        System.out.println("Filter 0 - encoding begins");
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //获取请求方法
        String path = request.getRequestURI();

        if (!path.contains("/login")){
            //设置响应字符编码为UTF-8
            response.setContentType("application/json;charset=UTF-8");

            String methon = request.getMethod();

            if ("POST-PUT-GET".contains(methon)) {
                //设置请求字符编码为UTF-8
                request.setCharacterEncoding("UTF-8");
            }
        }
        chain.doFilter(req,resp);
        System.out.println("continour");

    }

    @Override
    public void destroy() {

    }
}
